(ns acst.routes.news
  (:use acst.config.error-config
        acst.config.success-config
        clojure.test
        acst.utils.core)

  (:require [clojure.data.json :as json]
            [compojure.core :refer :all]
            [noir.session :as session]
            [acst.models.news :refer :all]
            [acst.models.users :refer [admin-require login-require set-user-session!]]))

(defn get-all-news [limit page]
  (->(let [[int_limit int_page] (parse-to-int  limit page)]
       (find-all-news int_limit int_page))
     (json/write-str)))

(defn get-news-by-id [news_id]
  (->
    (let [[int_news_id] (parse-to-int news_id)]
      (find-news-by-newid int_news_id))
    (json/write-str)))

(defn add-news! [title content]
  (admin-require
    (-> (let [publisher_id (session/get "user_id")]
                          (insert-news! publisher_id title content))
        (json/write-str))))

(defn remove-news! [news_id]
  (admin-require
    (-> (let [int_news_id (parse-to-int news_id)]
          (delete-news! (first int_news_id)))
        (json/write-str))))

(defn get-comments [news_id limit page]
  (->
    (let [[int_news_id int_limit int_page] (parse-to-int news_id limit page)]
      (cond
        (or (= int_news_id -1)
            (= int_limit -1)
            (= int_page -1)) (:comment-not-found comments-error)
        :else
        (find-news-comments int_news_id int_limit int_page)))
    (json/write-str)))

(defn add-comment! [news_id content reply_id]
  (login-require
    (->
      (let [user_id (session/get "user_id")
            [int_news_id int_reply_id] (parse-to-int news_id reply_id)]
        (cond
          (or (= int_news_id -1)
              (= int_reply_id -1))
          (:can-not-reply comments-error)
          :else
          (insert-news-comment! int_news_id user_id content int_reply_id)))
      (json/write-str))))

(defn get-comments-reply-tree [news_id comment_id]
  (->
    (let [[int_news_id int_comment_id] (parse-to-int news_id comment_id)]
      (cond
        (or (= int_news_id -1)
            (= int_comment_id -1))
        (:comment-not-found comments-error)
        :else
        (find-news-comments-reply-tree int_news_id int_comment_id)))
    (json/write-str)))

(defn remove-news-comment! [news_id comment_id]
  (login-require
    (->
      (let [[int_news_id int_comment_id] (parse-to-int news_id comment_id)]
        (delete-news-comment! int_news_id int_comment_id (session/get "user_id")))
      (json/write-str))))

(defn test-news []
  (set-user-session! "acst" 5)
  ;(get-news-by-id 6)
  ;(get-news-by-id "5")
  ;(add-news! nil nil)
  ;(add-comment! 6 "haha" nil)
  (add-comment! 6 "123123123" 6)
  ;(get-comments-reply-tree 6 2)
  ;(get-comments 6 20 1)
  ;(remove-news-comment! 6 1)
  ;(remove-news! "10")
  )

(defroutes news-routes
           (GET "/news" [page] (handler get-all-news nil 20 page 1))
           (POST "/news" [title content] (add-news! title content))
           (GET "/news/:news_id" [news_id] (handler get-news-by-id news_id nil))
           (DELETE "/news/:news_id" [news_id] (handler remove-news! news_id nil))
           (GET  "/news/:news_id/comment" [news_id page] (handler get-comments news_id nil nil 20 page 1))
           (POST "/news/:news_id/comment" [news_id content reply_id] (handler add-comment! news_id nil content nil reply_id nil))
           (GET  "/news/:news_id/comment/:comment_id" [news_id comment_id] (handler get-comments-reply-tree news_id nil comment_id nil))
           (DELETE "/news/:news_id/comment/:comment_id" [news_id comment_id] (handler remove-news-comment! news_id nil comment_id nil))

           (GET "/news/test/" [] (test-news)))